<?php

namespace app\appv110\controller;

use app\appv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\appv110\model\Custom;
use app\appv110\model\Order;
use app\appv110\model\OrdPro;
use app\appv110\model\StaffNote;
use app\appv110\model\Yuyue;
use app\appv110\model\YuyueCopy;

class OrderController extends CommonController {
    /*
     * code列表
     */

    public function code() {
        /*
         * 0  成功
         * 1  参数错误
         * 2  验证失败
         * 30000  
         * 30001  
         * 30002  
         */
    }

    /*
     * 订单详情
     * author xfwed
     * date 2019.11.07
     */

    public function ordInfo() {
        $rule = [
            'id' => 'require',
            'staff_id' => 'require',
        ];
        $msg = [
            'id.require' => '订单id必须',
            'staff_id.require' => '美容师id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $id = $all['id'];
        $staff_id = $all['staff_id'];

        $orderRow = Order::alias('o')
                ->join('custom c', 'c.id = o.ctm_id', 'left')
                ->join('coupon p', 'o.cun_id = p.id', 'left')
                ->where('o.id', $id)
                ->field('o.id,o.ctm_id,o.type,o.real_money,p.money,o.balance_money,o.red_money,o.pay_money,o.ord_num,o.pay_time,o.source,c.custom_phone,c.custom_name')
                ->find();
        $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $orderRow['ctm_id'])->field('name')->find();
        if ($notename['name']) {
            $orderRow['custom_name'] = $notename['name'];
        }
        $type = $orderRow['type'];
        if ($type == 1) {
            $prodata = OrdPro::alias('o')
                    ->join('item i', 'i.id = o.pro_id', 'left')
                    ->where('o.ord_id', '=', $orderRow['id'])
                    ->field('o.number,i.item_price,i.item_name,i.item_img')
                    ->find();
            $number = $prodata['number'];
            $price = $prodata['item_price'];
            $item_name = $prodata['item_name'];
            $item_img = $prodata['item_img'];
        } elseif ($type == 2) {
            $prodata = OrdPro::alias('o')
                    ->join('overgroup g', 'g.id = o.pro_id', 'left')
                    ->where('o.ord_id', '=', $orderRow['id'])
                    ->field('o.number,g.name,g.price')
                    ->find();
            $number = $prodata['number'];
            $price = $prodata['price'];
            $item_name = $prodata['name'];
            $item_img = 'http://47.104.83.107/images/kkmimg/kkmai/default/overgroup.png';
        } elseif ($type == 4) {
            $number = 1;
            $price = 9900;
            $item_name = '99特权卡';
            $item_img = 'http://47.104.83.107/images/kkmimg/kkmai/default/special.png';
        } elseif ($type == 5) {
            $prodata = OrdPro::alias('o')
                    ->join('miaosha m', 'm.id = o.pro_id', 'left')
                    ->join('item i', 'i.id = m.item_id', 'left')
                    ->where('o.ord_id', '=', $orderRow['id'])
                    ->field('i.item_name,m.price,o.number')
                    ->find();
            $number = $prodata['number'];
            $price = $prodata['price'];
            $item_name = $prodata['item_name'];
            $item_img = 'http://47.104.83.107/images/kkmimg/kkmai/default/miaosha.png';
        } elseif ($type == 6) {
            $number = 1;
            $price = 990;
            $item_name = '体验特权卡';
            $item_img = 'http://47.104.83.107/images/kkmimg/kkmai/default/special.png';
        }elseif ($type == 7) {
            $prodata = Db::name('order_yimei')
                    ->alias('o')
                    ->join('item i', 'i.id = o.item_id', 'left')
                    ->where('o.ord_id', '=', $orderRow['id'])
                    ->field('i.item_price,i.item_name,i.item_img')
                    ->find();
            $number = 1;
            $price = $prodata['item_price'];
            $item_name = $prodata['item_name'];
            $item_img = $prodata['item_img'];
        }elseif ($type == 8) {
            $prodata = OrdPro::alias('o')
                    ->join('overgroup g', 'g.id = o.pro_id', 'left')
                    ->where('o.ord_id', '=', $orderRow['id'])
                    ->field('o.number,g.name,g.price')
                    ->find();
            $number = $prodata['number'];
            $price = $prodata['price'];
            $item_name = $prodata['name'];
            $item_img = 'http://47.104.83.107/images/kkmimg/kkmai/default/overgroup.png';
        }
        $orderRow['number'] = $number;
        $orderRow['price'] = $price;
        $orderRow['item_name'] = $item_name;
        $orderRow['item_img'] = $item_img;
        return Utils::ajaxReturn(0, '查询成功', $orderRow);
    }

    /*
     * 历史订单(已退款)
     * author xfwed
     * date 2019.12.10
     */

    public function historyOrder() {
        $rule = [
            'staff_id' => 'require',
            'perPage' => 'integer|egt:1',
            'page' => 'integer|egt:1',
        ];
        $msg = [
            'staff_id.require' => '美容师id必须',
            'perPage.integer' => '每页数量为数字',
            'perPage.egt' => '每页数量最小为1',
            'page.integer' => '当前页数为数字',
            'page.egt' => '当前页数最小为1',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $staff_id = $all['staff_id'];
        $store_id = input('param.storeId');
        $dateType = input('param.dateType');//0本日 1本周 2本月 3全部  4自定义
        $startDate = input('param.startDate');
        $endDate = input('param.endDate');
        
        $perPage = isset($all['perPage']) ? $all['perPage'] : 10;
        $page = isset($all['page']) ? $all['page'] : 0;
        if ($page == 0) {
            $page = 1;
        }
        if($store_id){
            $where['y.store_id'] = $store_id;
        }
        if($dateType == 0){
            $start_time=strtotime(date("Y-m-d",time()));//当天开始时间
            $end_time=$start_time+60*60*24;//当天结束之间
            $startDate=date("Y-m-d H:i:s",$start_time);       
            $endDate=date("Y-m-d H:i:s",$end_time);  
        }elseif ($dateType == 1) {
            $date=date('Y-m-d');  //当前日期
            $first=1; //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
            $w=date('w',strtotime($date));  //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
            $startDate = date('Y-m-d H:i:s',strtotime("$date -".($w ? $w - $first : 6).' days')); //获取本周开始日期，如果$w是0，则表示周日，减去 6 天
            $endDate = date('Y-m-d H:i:s',strtotime("$startDate +6 days"));  //本周结束日期
        }elseif ($dateType == 2) {
            $startDate = date('Y-m-d H:i:s',mktime(0, 0, 0, date('m'), 1, date('Y')));
            $endDate = date('Y-m-d H:i:s',mktime(23, 59, 59, date('m'), date('t'), date('Y')));
        }elseif ($dateType == 4) {
            
        }
        if($startDate){
            $where['y.start_time'] = ['between',[$startDate,$endDate]];
        }

        $where['y.staff_id'] = $staff_id;
        $offset = ($page - 1) * $perPage;
        $num = Yuyue::alias('y')
                ->join('custom c', 'c.id=y.ctm_id', 'left')
                ->join('item i', 'i.id = y.item_id', 'left')
                ->join('store s', 's.id = y.store_id', 'left')
                ->where($where)
                ->where('y.refund_status', 2)
                ->field('y.id,y.order_id,y.start_time,y.ctm_id,c.custom_photo,c.vip_status,c.custom_phone,c.custom_name,i.item_name,s.store_name')
                ->count();
        $yuyueList = Yuyue::alias('y')
                ->join('custom c', 'c.id=y.ctm_id', 'left')
                ->join('item i', 'i.id = y.item_id', 'left')
                ->join('store s', 's.id = y.store_id', 'left')
                ->where($where)
                ->where('y.refund_status', 2)
                ->limit($offset, $perPage)
                ->field('y.id,y.order_id,y.start_time,y.ctm_id,c.custom_photo,c.vip_status,c.custom_phone,c.custom_name,i.item_name,s.store_name')
                ->select();
        $data = [];
        foreach ($yuyueList as $k => $v) {
            $notename = StaffNote::where('staff_id', $staff_id)->where('ctm_id', $v['ctm_id'])->field('name')->find();
            $detailId = YuyueCopy::where('yuyue_id',$v['id'])->order('status desc')->value('id');
            $data[$k]['statusStr'] = '已退款';
            $data[$k]['orderTime'] = $v['start_time'];
            $data[$k]['cusAvatar'] = $v['custom_photo'];
            $data[$k]['cusName'] = $v['custom_name'];
            $data[$k]['cusNameBak'] = $notename['name'];
            $data[$k]['cusPhone'] = $v['custom_phone'];
            $data[$k]['itemName'] = $v['item_name'];
            $data[$k]['orderStoreName'] = $v['store_name'];
            $data[$k]['isVip'] = $v['vip_status'];
            $data[$k]['yuyueId'] = $v['id'];
            $data[$k]['detailId'] = $detailId;
        }
        $returnData['total'] = $num;
        $returnData['dataList'] = $data;
        return Utils::ajaxReturn(0, '查询成功', $returnData);
    }

}
